package com.token.webservice;

import com.alibaba.fastjson.JSONObject;
import com.token.base.Pl;
import com.token.sql.IntegrateSql;
import lombok.Getter;
import lombok.Setter;

/**
 * @author chenjianxiong
 * @date 2020/7/8
 */
public abstract class WebServiceMainImpl  implements WebServiceMain {

    @Setter
    String username = "";

    @Setter
    String password = "";

    @Getter
    protected JSONObject inData;

    @Setter
    int actionTypeId;

    @Setter
    String actionTypeName;


    private final String actionTime;

    /**
     * 检查必填
     * @param value
     * @param title
     * @param fieldName
     * @return
     * @throws Exception
     */
    protected String needValue(String value, String title, String fieldName) throws Exception {
        if (value == null || "".equals(value)) {
            throw new Exception(title + "[" + fieldName + "] 字段必填！");
        }
        return value;
    }


    public WebServiceMainImpl(){
        actionTime = Pl.getCurDataTime();
        actionTypeId = 0;
        actionTypeName = null;
    }


    @Override
    public void writeLog(IntegrateSql iSql, String inStr, JSONObject result) throws Exception {
        iSql.setAutoCommit(true);
        FSysFlowApiLog log = new FSysFlowApiLog(iSql);
        log.setInsert();
        log.setSysId(Pl.getSysId());
        if(actionTypeName != null) {
            log.setActionType(actionTypeId); //操作类型
            log.setActionText(actionTypeName);
        }
        log.setStartTime(actionTime); //提交时间
        log.setUserName(username); //提交人
        log.setInParameter(inStr); //输入参数

        if(result != null) {
            log.setOutParameter(result.toJSONString()); //输出参数
            log.setCode(result.getIntValue("code"));
        }else{
            log.setCode(1);
        }
        log.setEndTime(Pl.getCurDataTime());
        log.executeUpdate();
        log.close();
    }
}
